package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dot.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("coursebase")
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

    @Override
    @PostMapping("/course/list")
    public PageVO queryCourseList(PageRequestParams params,
                       @RequestBody QueryCourseModel model) {
        //1.获得访问令牌并从中解析出机构的信息Id数据
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.queryCourseBaseList(params,model,companyId);
    }

    @Override
    @PostMapping("course")    //通过请求体传入参数 用Requestbody
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO vo) {
        //获取公司ID
        Long Companyid = SecurityUtil.getCompanyId();
        // 转换为dto
        CourseBaseDTO dto = CourseBaseConvert.INSTANCE.Voentity(vo);
        dto.setCompanyId(Companyid);

        CourseBaseDTO base = courseBaseService.createCourseBase(dto);
        return base;
    }



    @Override
    @GetMapping("course/{courseBaseId}")
    public CourseBaseDTO getCourseBase(@PathVariable Long courseBaseId) {
        //获取公司ID值
        Long companyId = SecurityUtil.getCompanyId();
        //获取课程相关信息
        return courseBaseService.getCourseBase(courseBaseId,companyId);
    }

    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //将Vo数据转为DTO
        CourseBaseDTO voentity = CourseBaseConvert.INSTANCE.Voentity(courseBaseVO);
        //获取公司ID
        Long companyId = SecurityUtil.getCompanyId();
        voentity.setCompanyId(companyId);

        return courseBaseService.modifyCourseBase(voentity);
    }


    @Override
    @DeleteMapping("course/{courseBaseId}")
    public void deleteCourse(Long courseBaseId) {

        Long companyId = SecurityUtil.getCompanyId();

        courseBaseService.deleteCourse(courseBaseId,companyId);
    }


    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }
}
